﻿using Org.BouncyCastle.Utilities;
using System;
using SZHelperCore;
using SZHelperCore.GMSM;

namespace SZHelperCore
{
    public class CryptoHelper
    {
        string publicKey = "";
        string privateKey = "";
        ConfigHelper config = new ConfigHelper("config_crypto.txt");
        SM2Utils sm2;
        public CryptoHelper()
        {
            config.Get("Config", "CryptoType", "SM2");
            publicKey = config.Get("Config", "PublicKey", "");
            privateKey = config.Get("Config", "PrivateKey", "");
            if (string.IsNullOrEmpty(publicKey)|| string.IsNullOrEmpty(privateKey))
            {
                ReGenCryptoKey();
            }
            sm2=new SM2Utils(publicKey, privateKey);
        }
        public string GetPublicKey()
        {
            return publicKey;
        }
        public void ReGenCryptoKey()
        {
            SM2Utils.GenerateKeyPair(out publicKey, out privateKey);
            config.Set("Config", "PublicKey", publicKey);
            config.Set("Config", "PrivateKey", privateKey);
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Decrypt(string data)
        {
            var result = sm2.Decrypt(data);
            if (result.Contains("\uFFFD"))
            {
                throw new Exception("解密失败");
            }
            return result;
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Encrypt(string data)
        {
            return sm2.Encrypt(data);
        }
        public string GetPassword(string txtPwd)
        {
            string strRandom;

            //获取3位随机字母
            Random r = new Random();
            strRandom = ((char)(r.Next(65, 65 + 26))).ToString() + ((char)(r.Next(65, 65 + 26))).ToString() + ((char)(r.Next(65, 65 + 26))).ToString();
            var result= (strRandom + SM3Utils.StrSM3(strRandom + txtPwd)).Substring(0, 64);//设定加密方式，也可以在此进行多次Md5加强密码安全度;
            return result;
        }
        public string GetSM3(string str)
        {
            return SM3Utils.StrSM3(str);
        }
        public string GetFileSM3(byte[] bytes)
        {
           return SM3Utils.ByteSM3(bytes);
        }
        public bool PasswordIsRight(string sqlPwd, string txtPwd)
        {
            if(string.IsNullOrEmpty(sqlPwd)||sqlPwd.Length!=64)
                return false;
            string str = sqlPwd.Substring(0, 3);
            string tmp = (str + SM3Utils.StrSM3(str + txtPwd)).Substring(0, 64);
            var result= sqlPwd == tmp ? true : false;
            return result;
        }
        /// <summary>
        /// 计算密码强度
        /// </summary>
        /// <param name="password">密码字符串</param>
        /// <returns></returns>
        public int PasswordStrength(string password)
        {

            var zx = new Zxcvbn.Zxcvbn();
           return zx.EvaluatePassword(password).Score;
        }
    }
}
